﻿
namespace SUIE
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint.WebPartPages;
    using SUIE.ControlTemplates;

    public class AccordionConfigurationEditorPart : EditorPart
    {
        // The deployment path of the User Control
        const string AccordionControlConfigurationPath = @"~/_CONTROLTEMPLATES/SUIE/AccordionConfigurationEditorPartUserControl.ascx";

        // The User Control Object ID
        const string UserControlID = "OperationsUserControl";        

        // Declare a reference to the User Control
        AccordionConfigurationEditorPartUserControl configuratorControl;

        public AccordionConfigurationEditorPart()
        {
            this.Title = "Configuration";            
        }

        public override bool ApplyChanges()
        {
            base.EnsureChildControls();

            var wb = this.WebPartToEdit as Accordion;
            if (wb == null) return false;

            // Set the Web Part's Properties
            wb.AccordionEnabled = configuratorControl.AccordionEnabled;
            wb.AccordionFirstItemOpened = configuratorControl.AccordionFirstItemOpen;
            wb.AccordionAutoHeight = configuratorControl.AccordionAutoHeight;
            wb.AccordionClearStyle = configuratorControl.AccordionClearStyle;
            wb.AccordionCollapsible = configuratorControl.AccordionCollapsible;
            wb.AccordionIcon = configuratorControl.AccordionIcon;
            wb.AccordionEvent = configuratorControl.AccordionEvent;
            wb.AccordionTheme = configuratorControl.AccordionTheme;

            return true;
        }

        public override void SyncChanges()
        {
            // sync with the new property changes here
            base.EnsureChildControls();
                        
            var wb = this.WebPartToEdit as Accordion;
            if (wb == null) return;
            
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // Load the User Control and it to the Controls Collection of the Editor Part
            this.configuratorControl =
                this.Page.LoadControl(AccordionConfigurationEditorPart.AccordionControlConfigurationPath) as AccordionConfigurationEditorPartUserControl;
            this.configuratorControl.ID = AccordionConfigurationEditorPart.UserControlID;

            var wb = this.WebPartToEdit as Accordion;
            if (wb == null) return;
            
            // set the user control properties, from the web part
            configuratorControl.AccordionEnabled = wb.AccordionEnabled;
            configuratorControl.AccordionFirstItemOpen = wb.AccordionFirstItemOpened;
            configuratorControl.AccordionAutoHeight = wb.AccordionAutoHeight;
            configuratorControl.AccordionClearStyle = wb.AccordionClearStyle;
            configuratorControl.AccordionCollapsible = wb.AccordionCollapsible;
            configuratorControl.AccordionIcon = wb.AccordionIcon;
            configuratorControl.AccordionEvent = wb.AccordionEvent;            
            configuratorControl.AccordionTheme = wb.AccordionTheme;
            Controls.Add(configuratorControl);
        }

    }
}
